//
//  KKDialogTextViewController.m
//  PhnixHome
//
//  Created by Levy Xu on 2018/1/8.
//  Copyright © 2018年 Levy Xu. All rights reserved.
//

#import "KKDialogTextViewController.h"

@interface KKDialogTextViewController ()<QMUITextViewDelegate>
@property(nonatomic, strong, readwrite) QMUITextView *textView;
@end

@implementation KKDialogTextViewController

- (void)didInitialized {
    [super didInitialized];
    
    self.enablesSubmitButtonAutomatically = YES;
    BeginIgnoreAvailabilityWarning
    [self loadViewIfNeeded];
    EndIgnoreAvailabilityWarning
}

- (void)initSubviews {
    [super initSubviews];
    
    self.textView = [[QMUITextView alloc] init];
    self.textView.backgroundColor = UIColorWhite;
    self.textView.delegate = self;
    self.textView.maximumTextLength = 240;
    self.textView.font = CodeFontMake(16);
    self.textView.returnKeyType = UIReturnKeyDone;
    self.textView.enablesReturnKeyAutomatically = _enablesSubmitButtonAutomatically;
    [self.view addSubview:self.textView];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.textView.frame = CGRectMake(0, CGRectGetMaxY(self.headerView.frame), CGRectGetWidth(self.view.bounds), (!self.footerView.hidden ? CGRectGetMinY(self.footerView.frame) : CGRectGetHeight(self.view.bounds)) - CGRectGetMaxY(self.headerView.frame));
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.textView becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.textView resignFirstResponder];
}

#pragma mark - Submit Button Enables

- (void)setEnablesSubmitButtonAutomatically:(BOOL)enablesSubmitButtonAutomatically {
    _enablesSubmitButtonAutomatically = enablesSubmitButtonAutomatically;
    self.textView.enablesReturnKeyAutomatically = _enablesSubmitButtonAutomatically;
    if (_enablesSubmitButtonAutomatically) {
        [self updateSubmitButtonEnables];
    }
}

- (void)updateSubmitButtonEnables {
    self.submitButton.enabled = [self shouldEnabledSubmitButton];
}

- (BOOL)shouldEnabledSubmitButton {
    if (self.shouldEnableSubmitButtonBlock) {
        return self.shouldEnableSubmitButtonBlock(self);
    }
    
    if (self.enablesSubmitButtonAutomatically) {
        NSInteger textLength = self.textView.text.qmui_trim.length;
        return 0 < textLength && textLength <= self.textView.maximumTextLength;
    }
    
    return YES;
}

-(void)textViewDidChange:(UITextView *)textView {
    if (self.textView == textView) {
        [self updateSubmitButtonEnables];
    }
}

- (void)addSubmitButtonWithText:(NSString *)buttonText block:(void (^)(__kindof QMUIDialogViewController *dialogViewController))block {
    [super addSubmitButtonWithText:buttonText block:block];
    [self updateSubmitButtonEnables];
}

#pragma mark - <QMUIModalPresentationContentViewControllerProtocol>

- (CGSize)preferredContentSizeInModalPresentationViewController:(QMUIModalPresentationViewController *)controller limitSize:(CGSize)limitSize {
    CGFloat textFieldHeight = 120;
    return CGSizeMake(limitSize.width, CGRectGetHeight(self.headerView.frame) + textFieldHeight + (!self.footerView.hidden ?  CGRectGetHeight(self.footerView.frame) : 0));
}

@end
